/*
 *  $Id: Tiles.java 4 2008-11-21 20:24:10Z Nathaniel.Waisbrot $
    Copyright (C) 2008  Nathaniel Waisbrot

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package termint.gui.graphics;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Display;

import termint.game.NHObject;
import termint.gui.vt.VTElement;

/**
 * Abstract class mapping something or other into a Tile image.
 * 
 * subclasses need to implement the fillData() method, called from the constructor
 * also, set tileSize to the dimensions of each tile
 * 
 * @author Nathaniel Waisbrot
 *
 */
public abstract class Tiles {
	protected Map<VTElement,Image> imageData;
	protected Map<VTElement,NHObject> objectData;

	protected Image nullImage;
	protected Display display;
	protected Color background;

	private Point tileSize;
	private Image cursor;
	
	Tiles(Display display, Color background) {
		this.display = display;
		this.imageData = new HashMap<VTElement,Image>();
		this.objectData = new HashMap<VTElement,NHObject>();
		this.background = background;
	}

	protected void setTileSize(int x, int y) {
		tileSize = new Point(x,y);
	}
	
	public Point getTileSize() {
		return tileSize;
	}
	public int getTileHeight() {
		return getTileSize().y;
	}
	public int getTileWidth() {
		return getTileSize().x;
	}
		
	protected void setImage(VTElement key, Image value) {
		Image i = imageData.get(key);
		if (i != null) {
			i.dispose();
		}
		imageData.put(key, value);
	}
	
	protected Image newImage() {
		return new Image(display, tileSize.x, tileSize.y);
	}
		
	public Image getImage(VTElement key) {
		Image i = imageData.get(key);
		if (i == null) {
			i = nullImage;
		}
		return i;
	}
	
	public NHObject getObjectData(VTElement e) {
		return objectData.get(e);
	}

	public void setObjectData(VTElement key, NHObject value) {
		objectData.put(key, value);
	}
	
	/**
	 * Call dispose() on all images contained in this Tiles
	 */
	public void dispose() {
		Iterator<Entry<VTElement, Image>> it = imageData.entrySet().iterator();
		while (it.hasNext()) {
			it.next().getValue().dispose();
		}
		nullImage.dispose();
		cursor.dispose();
	}
	
	/**
	 * Calls dispose()
	 */
	protected void finalize() {
		this.dispose();
	}

	/**
	 * @param cursor the cursor to set
	 */
	protected void setCursor(Image cursor) {
		this.cursor = cursor;
	}

	/**
	 * @return the cursor
	 */
	public Image getCursor() {
		return cursor;
	}
}
